﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;
using Runda.EShop.Common.Utility;
using System.Web.Services.Protocols;
using System.Linq;

namespace Runda.EShop.Web.Service
{
    /// <summary>
    /// Summary description for Order
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Order : System.Web.Services.WebService
    {
        public OrderSoapHeader myHeader = new OrderSoapHeader();

        /// <summary>
        /// WMS拆分订单并返回到订单系统
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        [SoapHeader("myHeader")]
        [WebMethod(Description = "WMS拆分订单并返回到订单系统")]
        public OperateResult InsertOrderDetailPatch(OrderDetailPatchWrapperEntity[] items)
        {
            List<OrderDetailPatchWrapperEntity> list = new List<OrderDetailPatchWrapperEntity>();

            list.AddRange(items);

            OperateResult result = new OperateResult();
            result.IsSuccess = true;
            if (myHeader.UserName == ConfigHelper.ReadConfig("SoapHeaderUserName") && myHeader.Password == ConfigHelper.ReadConfig("SoapHeaderPassword"))
            {
                OrderBll bll = new OrderBll();

                try
                {
                    var itemGroup = list.GroupBy(x => x.OrderID.ToLower())
                        .Select(group => new { OrderID = group.Key });

                    foreach (var group in itemGroup)
                    {
                        List<OrderDetailPatchWrapperEntity> subList = list.FindAll(item => item.OrderID.Equals(group.OrderID, StringComparison.OrdinalIgnoreCase));

                        bll.PatchOrder(subList.ToArray());
                    }
                }
                catch(Exception ex)
                {
                    result.IsSuccess = false;
                    result.Message = ex.Message;
                    Runda.EShop.Common.Utility.LogHelper.Write(ex.StackTrace,ex.Message, LogCategorySource.WMSServiceLog);
                }

                try
                {
                    string temp = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(items);
                    Runda.EShop.Common.Utility.LogHelper.Write(temp, "方法：InsertOrderDetailPatch", LogCategorySource.WMSServiceDataLog);
                }
                catch
                { }
            }
            else
            {
                result.IsSuccess = false;
                result.Message = "对不起，您没有权限调用此服务！ ";
            }

            return result;
        }


        [SoapHeader("myHeader")]
        [WebMethod(Description = "订单状态返回")]
        public OperateResult InsertOrderProcess(OrderProcessWrapperEntity entity)
        {
            LogHelper.Write(DateTime.Now.ToString() + "调用", LogCategorySource.WMSServiceLog);
            OperateResult result = new OperateResult();
            result.IsSuccess = true;
            if (myHeader.UserName == ConfigHelper.ReadConfig("SoapHeaderUserName") && myHeader.Password == ConfigHelper.ReadConfig("SoapHeaderPassword"))
            {
                try
                {
                    BizOrderProcessBll bll = new BizOrderProcessBll();
                    bll.InsertOrderProcess(entity);
                }
                catch (Exception ex)
                {
                    result.IsSuccess = false;
                    result.Message = ex.Message;
                    Runda.EShop.Common.Utility.LogHelper.Write(ex.Message, LogCategorySource.WMSServiceLog);
                }
                
                try
                {
                    string temp = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(entity);
                    Runda.EShop.Common.Utility.LogHelper.Write(temp, "方法：InsertOrderDetailPatch", LogCategorySource.WMSServiceDataLog);
                }
                catch
                { }
            }
            else
            {
                result.IsSuccess = false;
                result.Message = "对不起，您没有权限调用此服务！ ";
            }

            return result;
        }
    }
}
